package com.pinda.dao;

import java.math.BigInteger;
import java.util.UUID;

public class UUIDToDigits {
    public  String uuidToDigits() {
        // 生成UUID对象
        UUID uuid = UUID.randomUUID();
        // 获取字节数组表示
        byte[] bytes = toBytes(uuid);
        // 将字节数组转换为BigInteger对象
        BigInteger bigInteger = new BigInteger(1, bytes);
        // 获取BigInteger对象的字符串表示（十进制转换）
        String digits = bigInteger.toString();
        return  digits.substring(0,20);
    }

    private static byte[] toBytes(UUID uuid) {
        long mostSigBits = uuid.getMostSignificantBits();
        long leastSigBits = uuid.getLeastSignificantBits();

        byte[] bytes = new byte[16];
        for (int i = 0; i < 8; i++) {
            bytes[i] = (byte) (mostSigBits >>> 8 * (7 - i));
            bytes[8 + i] = (byte) (leastSigBits >>> 8 * (7 - i));
        }

        return bytes;
    }

}